<template>
  <div class="menu-wrapper">
    <el-form ref="form" :model="form" :rules="rules" label-width="140px">
      <div
        style="width: 100%;display: flex;align-items: flex-end;border-bottom: 1px #eee solid;margin-bottom: 20px"
      >
        <span style="font-size: 18px;color:#409EFF;margin: 10px 0;">基本信息</span>
      </div>

      <el-form-item label="公司名称:" prop="name">
        <el-col :span="6">
          <el-input v-model="form.name" clearable/>
        </el-col>
      </el-form-item>
      <el-form-item label="logo:" prop="logo">
        <single-upload :singleUploadTitle="singleUploadTitle" v-model="form.logo"
                       style="display: inline-block; width: 500px" v-if="imgShow==true"></single-upload>
        <img :src="form.logo" style="width:70px;height:70px" alt="" v-if="form.logo!=null&&imgShow==false">
        <span v-if="form.logo==null&&imgShow==false">{{ $t('sysMch.edit.noneAtPresent') }}</span>
      </el-form-item>
      <el-form-item label="客户类型:" prop="type">
        <el-col :span="6">
          <el-select style="width: 100%" v-model="form.type" clearable placeholder="请选择客户类型">
            <el-option v-for="item in typelist" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-col>
      </el-form-item>
      <el-form-item label="企业类型:" prop="type">
        <el-col :span="6">
          <el-select style="width: 100%" v-model="form.nature" placeholder="请选择企业类型">
            <el-option v-for="item in naturelist" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-col>
      </el-form-item>

      <el-form-item label="所属账户:" prop="companyId">
        <el-col :span="6">
          <query-company @queryCompanyClick='queryCompanyClick' :name="form.companyName"/>
        </el-col>
      </el-form-item>
      <el-form-item label="所属集团:" prop="groupId">
        <el-col :span="6">
          <query-group @queryGroupClick='queryGroupClick' :groupName="form.groupName" :group-id="form.groupId"/>
        </el-col>
      </el-form-item>
      <el-form-item label="联系人:" prop="linkman">
        <el-col :span="6">
          <el-input v-model="form.linkman" clearable/>
        </el-col>
      </el-form-item>
      <el-form-item label="联系电话:" prop="phone">
        <el-col :span="6">
          <el-input v-model="form.phone" clearable/>
        </el-col>
      </el-form-item>
      <el-form-item label="详细地址:" prop="address">
        <el-col :span="6">
          <el-input v-model="form.address" clearable/>
        </el-col>
      </el-form-item>
      <div style="width: 100%;display: flex;align-items: flex-end;border-bottom: 1px #eee solid;margin-bottom: 20px">
        <span style="font-size: 18px;color:#409EFF;margin: 10px 0;">资质信息</span>
      </div>
      <el-form-item label="统一社会信用代码:" prop="meta">
        <el-col :span="6">
          <el-input v-model="form.metaInfo.licenseCode"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="营业执照:" prop="license">
        <multi-upload :singleUploadTitle="singleUploadTitle" v-model="form.metaInfo.license"
                       style="display: inline-block;  width: 500px" v-if="imgShow==true"></multi-upload>
        <img :src="form.metaInfo.license" style="width:70px;height:70px" alt=""
             v-if="form.metaInfo.license!=null&&imgShow==false">
        <span v-if="form.metaInfo.license==null&&imgShow==false">{{ $t('sysMch.edit.noneAtPresent') }}</span>
      </el-form-item>
      <el-form-item label="危险化学品经营许可证:" prop="chemical">
        <multi-upload :singleUploadTitle="singleUploadTitle" v-model="form.metaInfo.chemical"
                       style="display: inline-block;  width: 500px" v-if="imgShow==true"></multi-upload>
        <img :src="form.metaInfo.chemical" style="width:70px;height:70px" alt=""
             v-if="form.metaInfo.chemical!=null&&imgShow==false">
        <span v-if="form.metaInfo.chemical==null&&imgShow==false">{{ $t('sysMch.edit.noneAtPresent') }}</span>
      </el-form-item>
      <el-form-item label="批发经营证书:" prop="wholesale">
        <multi-upload :singleUploadTitle="singleUploadTitle" v-model="form.metaInfo.wholesale"
                       style="display: inline-block;  width: 500px" v-if="imgShow==true"></multi-upload>
        <img :src="form.metaInfo.wholesale" style="width:70px;height:70px" alt=""
             v-if="form.metaInfo.wholesale!=null&&imgShow==false">
        <span v-if="form.metaInfo.wholesale==null&&imgShow==false">{{ $t('sysMch.edit.noneAtPresent') }}</span>
      </el-form-item>
      <el-form-item label="其他证件" prop="other">
        <multi-upload :singleUploadTitle="singleUploadTitle" v-model="form.metaInfo.other"
                       style="display: inline-block;  width: 500px" v-if="imgShow==true"></multi-upload>
        <img :src="form.metaInfo.other" style="width:70px;height:70px" alt=""
             v-if="form.metaInfo.other!=null&&imgShow==false">
        <span v-if="form.metaInfo.other==null&&imgShow==false">{{ $t('sysMch.edit.noneAtPresent') }}</span>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :loading="clicked" v-permission="['customer.add']"
                   @click="saveCustomer('form')">保 存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<style scoped>
.menu-wrapper {
  margin: 15px;
}
</style>
<script>
import {EventBus} from "@/api/eventBus";
import utils from "../../utils/utils.js";
import {save} from "@/api/customer";
import md5 from "js-md5";
import QueryGroup from "@/components/Query/queryGroup";
import QueryCompany from "@/components/Query/queryCompany";
import Security from "@/views/customer/components/Security";
import officeDialog from '@/components/OfficeDialog'
import singleUpload from "@/components/Upload/singleUpload";
import multiUpload from "@/components/Upload/multiUpload";
import permission from "@/directive/permission/index.js"; // 权限判断指令

export default {
  name: "customer-add",
  directives: {permission},

  filters: {
    // customerId: function(value){
    //   return value.toString();
    // },
    watchType(val) {
      if (val == 1) {
        return '供应商'
      } else {
        return '采购方'
      }
    },
    watchNature(val) {
      if (val == 1) {
        return '贸易公司'
      } else if (val == 2) {
        return '零售终端'
      } else {
        return '用油企业'
      }
    },
    watchStatus(val) {
      if (val == 1) {
        return '已认证'
      } else {
        return '未认证'
      }
    },
  },
  components: {
    Security,
    QueryGroup,
    QueryCompany,
    officeDialog,
    singleUpload,
    multiUpload
  },
  data() {
    return {
      activeTab: 'security',
      shippingDate: {},
      companyList: [],
      groupList: [],
      userList: [],
      pageNum: 1,
      pageSize: 20,
      userId: '',
      userName: '',
      imgShow: true,
      singleUploadTitle: "100*100",
      clicked: false, // 按钮防双击
      form: {
        name: "",        //公司名称
        logo: "",         //公司Logo
        address: "",	    //详情地址
        linkman: "",	    //联系人
        phone: "",        //联系电话
        companyId: '',    //所属账户
        groupId: '',     //所属集团
        meta: "",         //审核资料
        metaInfo: {
          chemical: "",
          license: "",
          licenseCode: null,
          other: "",
          wholesale: "",
        },
        msg: "",          //审核说明
        status: 0,        //状态(0.未认证,1.已认证)
        type: '',       //客户类型(1.供应商,2.采购方)

      },
      rules: {
        name: [
          {required: true, message: "请输入公司名称", trigger: "blur"},
          {min: 2, max: 20, message: "长度在 2 到 20 个字符", trigger: "blur"}
        ],
        type: [
          {required: true, message: "请选择公司类型", trigger: "blur"}
        ],
        linkman: [
          {required: true, message: "请输入联系人", trigger: "blur"}
        ],
        phone: [
          {required: true, message: "请输入联系电话", trigger: "blur"}
        ],
        companyId: [
          {required: true, message: "请选择所属账户", trigger: "blur"}
        ],
        groupId: [
          {required: true, message: "请选择所属集团", trigger: "blur"}
        ],
        address: [
          {required: true, message: "请输入详情地址", trigger: "blur"}
        ],
      },
      naturelist: [
        {
          id: 1,
          name: "贸易公司"
        }, {
          id: 2,
          name: "零售终端"
        }, {
          id: 3,
          name: "用油企业"
        }
      ],
      typelist: [
        {
          id: 1,
          name: "供应商"
        }, {
          id: 2,
          name: "采购方"
        }
      ],
    };
  },
  created() {
    var _this = this;
  },
  watch: { // 路由地址不变的情况下,如果id变化则重新获取数据
    // $route(newVal, oldVal) {
    //   if (newVal.name == 'customer-add' && newVal.query.id != this.id) {
    //     this.getData()
    //   }
    // },
  },
  methods: {
//    remoteMethod(query) {
//      this.loadCompanyData(query)
//      this.queryCompanyClick()
//      this.queryGroupClick()
//      this.selectType()
//
//    },
//
    queryCompanyClick(val) {
      // console.log("选择的是", val)
      this.form.companyId = val.companyId;
      this.form.companyName = val.name;
//      this.companyName = this.form.companyName
      console.log("选择的账户是", this.form.companyName)
    },
    queryGroupClick(val) {
      // console.log("选择的是", val)
      this.form.groupId = val.groupId
      this.form.groupName = val.name
//      this.groupName = this.form.groupName
      console.log("选择的集团是", this.form.groupName)
    },
    selectNature(e) {
      this.form.nature = e.id
    },

    saveCustomer(form) {
      const _this = this;
      console.log("form", this.$refs[form].model);
      // console.log("点击创建了", this.$refs[form],)
      this.$refs[form].validate(valid => {
        console.log("点击创建了", valid)
        if (valid) {
          // 校验表单
          var list = {
            name: _this.form.name,
            logo: _this.form.logo,
            status: _this.form.status,
            groupId: _this.form.groupId,
            companyId: _this.form.companyId,
            phone: _this.form.phone,
            linkman: _this.form.linkman,
            address: _this.form.address,
            meta: _this.form.meta,
            metaInfo: {
              chemical: _this.form.metaInfo.chemical,
              license: _this.form.metaInfo.license,
              licenseCode: _this.form.metaInfo.licenseCode,
              other: _this.form.metaInfo.other,
              wholesale: _this.form.metaInfo.wholesale,
            },
            // msg: _this.form.msg,
            type: _this.form.type,
            // type: _this.query.id,
          }
          _this.clicked = true;
          console.log(list)
          save(list)
            .then(response => {
              _this.clicked = false;
              if (response.type == "success") {
                let data = response.data;
                console.log("修改完返回给列表的数据--" + JSON.stringify(data));
                EventBus.$emit("customer-add", data);
                _this.$store
                  .dispatch("tagsView/delView", _this.$route)
                  .then(({visitedViews}) => {
                    _this.$router.push({
                      path: "/customer/list"
                    });
                  }); // 关闭当前tab回到列表
              } else {
                _this.$message({
                  message: response.content,
                  offset: 120,
                  center: true
                });
                console.log("content", response.content);

              }
            })
            .catch(error => {
              _this.clicked = false;
            });
        } else {
          return false;
        }
      });
    },
    //过滤成日期
    formatDate2Day(val) {
      let date = new Date(val);
      let y = date.getFullYear();
      let MM = date.getMonth() + 1;
      MM = MM < 10 ? "0" + MM : MM;
      let d = date.getDate();
      d = d < 10 ? "0" + d : d;
      return y + "-" + MM + "-" + d;
    },
    //过滤到时间
    formatDate2Time(val) {
      let date = new Date(val);
      let y = date.getFullYear();
      let MM = date.getMonth() + 1;
      MM = MM < 10 ? "0" + MM : MM;
      let d = date.getDate();
      d = d < 10 ? "0" + d : d;
      let h = date.getHours();
      h = h < 10 ? "0" + h : h;
      let m = date.getMinutes();
      m = m < 10 ? "0" + m : m;
      let s = date.getSeconds();
      s = s < 10 ? "0" + s : s;
      return y + "-" + MM + "-" + d + " " + h + ":" + m + ":" + s;
    },
    // 过滤企业列表
    formatList(data) {
      data.createDate = this.formatDate2Day(data.createDate);
      data.modifyDate = this.formatDate2Time(data.modifyDate);
      data.rechangeDate = this.formatDate2Time(data.rechangeDate);
      return data;
    },
  }
};
</script>
